package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class Main {
    public static void main(String[] args) throws AWTException, InterruptedException {


        while (true) {
            // 创建一个面板来放置输入组件
            JPanel panel = new JPanel(new GridLayout(0, 1));

            // 添加第一个输入框: 按键
            JTextField nameField = new JTextField();
            panel.add(new JLabel("请输入按键"));
            panel.add(nameField);
            // 添加第二个输入框: 每次延迟秒数
            JTextField wait = new JTextField();
            panel.add(new JLabel("请输入每次延迟时间（毫秒）: 默认2000毫秒"));
            panel.add(wait);
            // n秒后开始执行逻辑
            JTextField loading = new JTextField();
            panel.add(new JLabel("您希望在多少秒后开始执行: 默认5000毫秒"));
            panel.add(loading);
            // 每次按键的延迟时间
            JTextField buttonWait = new JTextField();
            panel.add(new JLabel("每次按键的延迟时间: 默认0毫秒（模拟真实操作，建议为100-300毫秒）"));
            panel.add(buttonWait);
            // 循环次数
            JTextField xunhuan = new JTextField();
            panel.add(new JLabel("循环次数: 默认十次"));
            panel.add(xunhuan);


            // 弹出对话框
            int result = JOptionPane.showConfirmDialog(null, panel, "请输入信息", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (result == JOptionPane.OK_OPTION) {
                // 用户点击了OK按钮
                String button = nameField.getText();

                // 判断是否为空
                if (button == null || "".equals(button)) {
                    JOptionPane.showMessageDialog(null, "请输入按键信息");
                    continue;
                }
                // 判断是否填写的为数字
                double randomNum = 0.15;
                int waitNum = 2000; // 延迟时间
                int loadingNum = 5000; // n秒后开始执行逻辑
                int xunhuanNum = 10; // 循环次数
                int buttonWaitNum = 0; // 每次按键的延迟时间
                try {
                    waitNum = Integer.valueOf(wait.getText());
                    loadingNum = Integer.valueOf(loading.getText());
                    xunhuanNum = Integer.valueOf(xunhuan.getText());
                    buttonWaitNum = Integer.valueOf(buttonWait.getText());
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "请输入数字");
                    continue;
                }

                String[] buttons = button.split(",");
                ArrayList<ArrayList<String>> buttonsList = new ArrayList<>();
                for (int i = 0; i < buttons.length; i++) {
                    ArrayList<String> buttonList = new ArrayList<>();
                    String[] buttonss = buttons[i].split("\\+");
                    for (int j = 0; j < buttonss.length; j++) {
                        buttonList.add(buttonss[j]);
                    }
                    buttonsList.add(buttonList);
                }


                ArrayList<ArrayList<Integer>> strListList = new ArrayList<>();
                ArrayList<ArrayList<Integer>> strListListReverse = new ArrayList<>();
                Robot robot = new Robot();
                for (ArrayList<String> strings : buttonsList) {
                    ArrayList<Integer> strList1 = new ArrayList<>();
                    strings.stream().map(word -> word.toUpperCase()).forEach(str -> {
                        int keyCode = 0;
                        switch (str) {
                            // 修饰键
                            case "SHIFT":
                                strList1.add(KeyEvent.VK_SHIFT);
                                break;
                            case "CTRL":
                            case "CONTROL":
                                strList1.add(KeyEvent.VK_CONTROL);
                                break;
                            case "ALT":
                                strList1.add(KeyEvent.VK_ALT);
                                break;
                            case "ALTGR":
                                strList1.add(KeyEvent.VK_ALT_GRAPH);
                                break;
                            case "META":
                            case "WIN":
                            case "COMMAND":
                                strList1.add(KeyEvent.VK_META);
                                break;

                            // 功能键
                            case "F1":
                                strList1.add(KeyEvent.VK_F1);
                                break;
                            case "F2":
                                strList1.add(KeyEvent.VK_F2);
                                break;
                            case "F3":
                                strList1.add(KeyEvent.VK_F3);
                                break;
                            case "F4":
                                strList1.add(KeyEvent.VK_F4);
                                break;
                            case "F5":
                                strList1.add(KeyEvent.VK_F5);
                                break;
                            case "F6":
                                strList1.add(KeyEvent.VK_F6);
                                break;
                            case "F7":
                                strList1.add(KeyEvent.VK_F7);
                                break;
                            case "F8":
                                strList1.add(KeyEvent.VK_F8);
                                break;
                            case "F9":
                                strList1.add(KeyEvent.VK_F9);
                                break;
                            case "F10":
                                strList1.add(KeyEvent.VK_F10);
                                break;
                            case "F11":
                                strList1.add(KeyEvent.VK_F11);
                                break;
                            case "F12":
                                strList1.add(KeyEvent.VK_F12);
                                break;
                            case "F13":
                                strList1.add(KeyEvent.VK_F13);
                                break;
                            case "F14":
                                strList1.add(KeyEvent.VK_F14);
                                break;
                            case "F15":
                                strList1.add(KeyEvent.VK_F15);
                                break;
                            case "F16":
                                strList1.add(KeyEvent.VK_F16);
                                break;
                            case "F17":
                                strList1.add(KeyEvent.VK_F17);
                                break;
                            case "F18":
                                strList1.add(KeyEvent.VK_F18);
                                break;
                            case "F19":
                                strList1.add(KeyEvent.VK_F19);
                                break;
                            case "F20":
                                strList1.add(KeyEvent.VK_F20);
                                break;
                            case "F21":
                                strList1.add(KeyEvent.VK_F21);
                                break;
                            case "F22":
                                strList1.add(KeyEvent.VK_F22);
                                break;
                            case "F23":
                                strList1.add(KeyEvent.VK_F23);
                                break;
                            case "F24":
                                strList1.add(KeyEvent.VK_F24);
                                break;

                            // 导航键
                            case "UP":
                            case "上":
                                strList1.add(KeyEvent.VK_UP);
                                break;
                            case "DOWN":
                            case "下":
                                strList1.add(KeyEvent.VK_DOWN);
                                break;
                            case "LEFT":
                            case "左":
                                strList1.add(KeyEvent.VK_LEFT);
                                break;
                            case "RIGHT":
                            case "右":
                                strList1.add(KeyEvent.VK_RIGHT);
                                break;
                            case "PAGEUP":
                                strList1.add(KeyEvent.VK_PAGE_UP);
                                break;
                            case "PAGEDOWN":
                                strList1.add(KeyEvent.VK_PAGE_DOWN);
                                break;
                            case "HOME":
                                strList1.add(KeyEvent.VK_HOME);
                                break;
                            case "END":
                                strList1.add(KeyEvent.VK_END);
                                break;
                            case "INSERT":
                                strList1.add(KeyEvent.VK_INSERT);
                                break;
                            case "DELETE":
                                strList1.add(KeyEvent.VK_DELETE);
                                break;

                            // 其他控制键
                            case "ENTER":
                            case "回车":
                                strList1.add(KeyEvent.VK_ENTER);
                                break;
                            case "ESC":
                            case "ESCAPE":
                                strList1.add(KeyEvent.VK_ESCAPE);
                                break;
                            case "TAB":
                                strList1.add(KeyEvent.VK_TAB);
                                break;
                            case "SPACE":
                            case "空格":
                                strList1.add(KeyEvent.VK_SPACE);
                                break;
                            case "BACKSPACE":
                                strList1.add(KeyEvent.VK_BACK_SPACE);
                                break;
                            case "CAPSLOCK":
                                strList1.add(KeyEvent.VK_CAPS_LOCK);
                                break;
                            case "NUMLOCK":
                                strList1.add(KeyEvent.VK_NUM_LOCK);
                                break;
                            case "SCROLLLOCK":
                                strList1.add(KeyEvent.VK_SCROLL_LOCK);
                                break;
                            case "PRINTSCREEN":
                                strList1.add(KeyEvent.VK_PRINTSCREEN);
                                break;
                            case "PAUSE":
                                strList1.add(KeyEvent.VK_PAUSE);
                                break;

                            // 数字键盘
                            case "NUMPAD0":
                            case "0":
                                strList1.add(KeyEvent.VK_NUMPAD0);
                                break;
                            case "NUMPAD1":
                            case "1":
                                strList1.add(KeyEvent.VK_NUMPAD1);
                                break;
                            case "NUMPAD2":
                            case "2":
                                strList1.add(KeyEvent.VK_NUMPAD2);
                                break;
                            case "NUMPAD3":
                            case "3":
                                strList1.add(KeyEvent.VK_NUMPAD3);
                                break;
                            case "NUMPAD4":
                            case "4":
                                strList1.add(KeyEvent.VK_NUMPAD4);
                                break;
                            case "NUMPAD5":
                            case "5":
                                strList1.add(KeyEvent.VK_NUMPAD5);
                                break;
                            case "NUMPAD6":
                            case "6":
                                strList1.add(KeyEvent.VK_NUMPAD6);
                                break;
                            case "NUMPAD7":
                            case "7":
                                strList1.add(KeyEvent.VK_NUMPAD7);
                                break;
                            case "NUMPAD8":
                            case "8":
                                strList1.add(KeyEvent.VK_NUMPAD8);
                                break;
                            case "NUMPAD9":
                            case "9":
                                strList1.add(KeyEvent.VK_NUMPAD9);
                                break;
                            case "ADD":
                            case "NUMPAD+":
                            case "+":
                                strList1.add(KeyEvent.VK_ADD);
                                break;
                            case "SUBTRACT":
                            case "NUMPAD-":
                            case "-":
                                strList1.add(KeyEvent.VK_SUBTRACT);
                                break;
                            case "MULTIPLY":
                            case "NUMPAD*":
                            case "*":
                                strList1.add(KeyEvent.VK_MULTIPLY);
                                break;
                            case "DIVIDE":
                            case "NUMPAD/":
                            case "/":
                                strList1.add(KeyEvent.VK_DIVIDE);
                                break;
                            case "DECIMAL":
                            case "NUMPAD.":
                                strList1.add(KeyEvent.VK_DECIMAL);
                                break;
                            case "SEPARATOR":
                                strList1.add(KeyEvent.VK_SEPARATOR);
                                break;

                            // 鼠标按钮
                            case "MOUSE1":
                            case "LEFTCLICK":
                            case "LBUTTON":
                            case "鼠标左键":
                            case "左键":
                                strList1.add(InputEvent.BUTTON1_DOWN_MASK);
                                break;
                            case "MOUSE2":
                            case "RIGHTCLICK":
                            case "RBUTTON":
                            case "鼠标右键":
                            case "右键":
                                strList1.add(InputEvent.BUTTON2_DOWN_MASK);
                                break;
                            case "MOUSE3":
                            case "MIDDLECLICK":
                            case "MBUTTON":
                            case "鼠标中键":
                            case "滚轮":
                            case "滚轮键":
                                strList1.add(InputEvent.BUTTON3_DOWN_MASK);
                                break;
                            default:
                                // 处理普通字母键
                                if (str.length() == 1) {
                                    strList1.add(KeyEvent.getExtendedKeyCodeForChar(str.charAt(0)));
                                }
                        }
                    });
                    if (strList1 != null && strList1.size() > 0) {
                        // 按下的list
                        strListList.add(strList1);
                        // 松手的list
                        strListListReverse.add(strList1);
                    } else {
                        JOptionPane.showMessageDialog(null, "按键不存在重新填写");
                        System.exit(0);
                    }
                }
                if (strListList == null || strListList.size() == 0) {
                    JOptionPane.showMessageDialog(null, "信息有误重新填写");
                    continue;
                }
                // n秒后开始执行按键逻辑
                Thread.sleep(loadingNum);
                try {
                    Random random = new Random();
                    // 执行按键/松键逻辑
                    for (int p = 0; p < xunhuanNum; p++) {
                        for (int i = 0; i < strListList.size(); i++) {
                            //TODO
                            // 判断为鼠标操作或键盘操作，此处浪费性能，后期修改
//                            if () {
//                                for (int j = 0; j < strListList.get(i).size(); j++) {
//                                    robot.mousePress(strListList.get(i).get(j));
//                                }
//                                for (int j = 0; j < strListList.get(i).size(); j++) {
//                                    robot.mouseRelease(strListList.get(i).get(j));
//                                }
//                            } else {
                            // 循環按下按鈕
                            for (int j = 0; j < strListList.get(i).size(); j++) {
                                robot.keyPress(strListList.get(i).get(j));
                                // 设置短时间随机值s
                                Thread.sleep(buttonWaitNum);
                            }
                            for (int j = 0; j < strListList.get(i).size(); j++) {
                                robot.keyRelease(strListList.get(i).get(j));
                                // 设置短时间随机值
                                Thread.sleep(buttonWaitNum);
                            }
                        }
                        // 延遲時間
                        Thread.sleep(waitNum);
                    }
                } catch (Exception e) {
                    throw new RuntimeException("点击按钮时发生错误", e);

                } finally {
                    System.exit(0);
                }
            } else {
                System.out.println("操作取消");
                System.exit(0);
            }
        }


    }
}